<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('模具报价')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a :loading="isButtonLoading" @click="handleSyc(record)">同步多维表格</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mold-quotation-modal ref="modalForm" @ok="modalFormOk"></mold-quotation-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import MoldQuotationModal from './modules/MoldQuotationModal'
import { deleteAction, getAction, putAction, postAction, httpAction, downFile, getFileAccessHttpUrl, printAction } from '@/api/manage'

export default {
  name: 'MoldQuotationList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    MoldQuotationModal
  },
  data() {
    return {
      description: '模具报价管理页面',
      isButtonLoading: false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '报价单号',
          align: "center",
          dataIndex: 'quoteNumber'
        },
        {
          title: '客户名称',
          align: "center",
          dataIndex: 'customerName'
        },
        {
          title: '产品名称',
          align: "center",
          dataIndex: 'productOrMoldName'
        },
        {
          title: '产品尺寸',
          align: "center",
          dataIndex: 'productSize'
        },
        {
          title: '产品材料',
          align: "center",
          dataIndex: 'productMaterial'
        },
        {
          title: '图片',
          align: "center",
          dataIndex: 'pic',
          scopedSlots: { customRender: 'imgSlot' }
        },
        {
          title: '附件',
          align: "center",
          dataIndex: 'attachment',
          scopedSlots: { customRender: 'fileSlot' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/ch/moldQuotation/list",
        delete: "/ch/moldQuotation/delete",
        deleteBatch: "/ch/moldQuotation/deleteBatch",
        exportXlsUrl: "/ch/moldQuotation/exportXls",
        importExcelUrl: "ch/moldQuotation/importExcel",
        queryAccessToken: "/ch/oAuthToken/queryAccessToken",
        feishuSycAddUrl: "/ch/oAuthToken/feishuSycAddUrl",

      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    const code = this.$route.query.code;
    localStorage.setItem('yxxCode', code); // 存储
    // 动态修改 url 中的路径（例如为 importExcelUrl 添加 code 参数）
    this.url.importExcelUrl = `${this.url.importExcelUrl}?code=${code}`;

    this.queryAccessToken();//给后端传code获取AccessToken
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      var code = localStorage.getItem('yxxCode'); // 读取
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}?code=${code}`;
    },
  },
  
  methods: {
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'quoteNumber', text: '报价单号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'customerName', text: '客户名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'productOrMoldName', text: '产品名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'productSize', text: '产品尺寸', dictCode: '' })
      fieldList.push({ type: 'string', value: 'productMaterial', text: '产品材料', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'pic', text: '图片', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'attachment', text: '附件', dictCode: '' })
      this.superFieldList = fieldList
    },
    queryAccessToken() {
      const code = this.getUserIdCode();
      if (!code) {
        return;
      }
      getAction(this.url.queryAccessToken, { code, code }).then((res) => {
        if (res.success) {
          console.log("传送code成功", code);
          console.log("获取AccessToken成功", res);
        }
      })
    },
    getUserIdCode() {
      return localStorage.getItem('yxxCode'); // 读取
    },
    /**
     * 同步多维表格
     * @param record 
     */
    handleSyc(record) {
      // try {
      //   this.isButtonLoading = true;
      //   const code = this.getUserIdCode();
      //   if (!code) {
      //     return;
      //   }
      //   console.log("该行记录", record);
      //   // 构建请求参数
      //   const payload = {
      //     code: code,
      //     record: record  // 整行记录对象
      //   };
      //   postAction(this.url.feishuSycAddUrl + "?code=" + code, record).then((res) => {
      //     if (res.success) {
      //       console.log("feishuSycAddUrl 传送code成功", res);
      //       this.$message.success('同步飞书记录成功')
      //     } else {
      //       this.$error({ title: '同步飞书记录异常', content: '错误信息：' + res.message, maskClosable: true })
      //     }
      //   })

      // } catch (error) {
      //   console.error(error);
      //   this.$error({ title: '同步飞书记录异常', content: '错误信息：' + res.message, maskClosable: true })
      // } finally {
      //   this.isButtonLoading = false;
      // }

      //读取json文件
      fetch('src/assets/json/book.json')
        .then(response => response.json())
        .then(data => {
          console.log(data);
          // 在这里处理从json文件中读取的数据
        })
        .catch(error => {
          console.error('Error:', error);
        });
    }
  }
}


</script>
<style scoped>
@import '~@assets/less/common.less';
</style>